package top.MiraiJavaEasy;

import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.utils.BotConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.MiraiJavaEasy.Exception.MiraiJavaEasyRuntimeException;

import java.io.File;

/**
 * Mirai配置类
 */
public class MiraiConfiguration {
    private final Logger logger =  LoggerFactory.getLogger(MiraiConfiguration.class);
    /**
     * QQ号
     */
    private Long QQNumber;
    /**
     * QQ密码
     */
    private String QQPassword;
    /**
     * 运行目录，不填则遵循mirai默认配置，默认在 new File(".")
     */
    private File RunDirectory;
    /**
     * 设备信息的存储文件名，不填则遵循mirai默认配置，默认为 device.json
     */
    private String DeviceInformation;
    /**
     * 登录协议，不填则遵循mirai默认配置，默认使用 ANDROID_PHONE
     */
    private BotConfiguration.MiraiProtocol LoginAgreement;
    /**
     * 生成的bot对象
     */
    private Bot bot;

    /**
     * 加载配置并且创建Bot
     *
     * @return 创建的Bot对象
     */
    public Bot Load() {
        if (QQNumber == null || QQPassword == null) {
            throw new MiraiJavaEasyRuntimeException("账号或密码未填写！");
        } else {
            try {
                logger.info("开始创建Bot...");
                bot = BotFactory.INSTANCE.newBot(QQNumber, QQPassword, new BotConfiguration() {{
                    //配置运行目录
                    if (RunDirectory != null) {
                        setWorkingDir(RunDirectory);
                    }
                    //配置设备信息的存储文件名
                    if (DeviceInformation != null) {
                        fileBasedDeviceInfo(DeviceInformation);
                    }
                    //配置登录协议
                    if (LoginAgreement != null) {
                        setProtocol(LoginAgreement);
                    }
                }});
                logger.info("Bot " + QQNumber + " 创建成功！");
                return bot;
            } catch (Exception e) {
                throw new MiraiJavaEasyRuntimeException("Bot创建失败！", e);
            }
        }
    }

    /**
     * 登入Bot
     *
     * @return Bot对象
     */
    public Bot Login() {
        try {
            logger.info("Bot " + QQNumber + " 开始登入！");
            bot.login();
            logger.info("Bot " + QQNumber + " 登入成功！");
            return bot;
        } catch (Exception e) {
            throw new MiraiJavaEasyRuntimeException("Bot " + QQNumber + "登入失败！", e);
        }
    }

    /**
     * 加载Bot后直接登入
     *
     * @return Bot对象
     */
    public Bot LoadAndLogin() {
        Load();
        Login();
        return bot;
    }


    public MiraiConfiguration() {
    }

    public MiraiConfiguration(Long QQNumber, String QQPassword) {
        this.QQNumber = QQNumber;
        this.QQPassword = QQPassword;
    }

    public Long getQQNumber() {
        return QQNumber;
    }

    public void setQQNumber(Long QQNumber) {
        this.QQNumber = QQNumber;
    }

    public String getQQPassword() {
        return QQPassword;
    }

    public void setQQPassword(String QQPassword) {
        this.QQPassword = QQPassword;
    }

    public File getRunDirectory() {
        return RunDirectory;
    }

    public void setRunDirectory(File runDirectory) {
        RunDirectory = runDirectory;
    }

    public String getDeviceInformation() {
        return DeviceInformation;
    }

    public void setDeviceInformation(String deviceInformation) {
        DeviceInformation = deviceInformation;
    }

    public BotConfiguration.MiraiProtocol getLoginAgreement() {
        return LoginAgreement;
    }

    public void setLoginAgreement(BotConfiguration.MiraiProtocol loginAgreement) {
        LoginAgreement = loginAgreement;
    }

    public Bot getBot() {
        return bot;
    }
}
